import Vue from 'vue'
import moment from 'moment'
import 'moment/locale/zh-cn'
import store from '@/store'
import { SysAreaCodeAreas, SysAreaCode } from '@/utils/areaCode'

moment.locale('zh-cn')

Vue.filter('NumberFormat', function (value) {
  if (!value) {
    return '0'
  }
  const intPartFormat = value.toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1,') // 将整数部分逢三一断
  return intPartFormat
})

Vue.filter('dayjs', function (dataStr, pattern = 'YYYY-MM-DD HH:mm:ss') {
  return moment(dataStr).format(pattern)
})

Vue.filter('empty', function (dataStr) {
  if (dataStr === '' || dataStr === undefined) {
    return '无'
  } else {
    return dataStr
  }
})

Vue.filter('moment', function (dataStr, pattern = 'YYYY-MM-DD HH:mm:ss') {
  return moment(dataStr).format(pattern)
})

/**
 * 根据6位地区编码返回省市区名称
 */
Vue.filter('areaCode', function (code) {
  let area
  let city
  let province
  for (const item of SysAreaCodeAreas()) {
    if (item.code === code) {
      area = item
      for (const provinceTmp of SysAreaCode()) {
        if (provinceTmp.code === area.provinceCode) {
          province = provinceTmp
          for (const cityTmp of provinceTmp.children) {
            if (cityTmp.code === area.cityCode) {
              city = cityTmp
              return province.name + city.name + area.name
            }
          }
        }
      }
    }
  }
})

/**
 * 根据6位地区编码返回省市区名称 区名称
 */
export const AreaCode3level = (departCode) => {
  let area
  for (const item of SysAreaCodeAreas()) {
    if (item.code === departCode) {
      area = item
      for (const provinceTmp of SysAreaCode()) {
        if (provinceTmp.code === area.provinceCode) {
          for (const cityTmp of provinceTmp.children) {
            if (cityTmp.code === area.cityCode) {
              return cityTmp.name + area.name
            }
          }
        }
      }
    }
  }
}

Vue.filter('dataFilter', function (key, type) {
  if (key !== undefined && key !== '') {
    const dictListForType = store.getters.sysDictListForType(type)
    if (dictListForType) {
      if (dictListForType['wx' + key]) {
        return dictListForType['wx' + key].v
      }
    }
  }
  return ''
})
Vue.filter('getDepartmentName', function (departCode) {
  if (departCode !== undefined && departCode !== '') {
    return store.getters.getDepartmentName(departCode)
  }
  return ''
})

Vue.filter('getDictFilterName', function (key, type) {
  if (key !== undefined && key !== '') {
    const dictListForType = store.getters.sysDictListForType(type)
    if (dictListForType) {
      if (dictListForType['wx' + key]) {
        return dictListForType['wx' + key].v
      }
    }
  }
  return ''
})

/**
 * sysdict 字典表
 * @param key key
 * @param type 类型
 * @returns {string|*} value值
 */
export const SysDictFilter = (key, type) => {
  if (key !== undefined && key !== '') {
    const dictListForType = store.getters.sysDictListForType(type)
    if (dictListForType) {
      if (dictListForType['wx' + key]) {
        return dictListForType['wx' + key].v
      }
    }
  }
  return ''
}

/**
 * sysdict 字典表 数组
 * @param key key
 * @param type 类型
 * @returns {string|*} value值
 */
export const SysDictArrayFilter = (key, type) => {
  var result = ''
  if (key !== undefined && key !== '') {
    const dictListForType = store.getters.sysDictListForType(type)
    var arrys = key.split(',')
    for (const arrysKey in arrys) {
      if (dictListForType) {
        if (dictListForType['wx' + arrys[arrysKey]]) {
          result = result + dictListForType['wx' + arrys[arrysKey]].v + ','
        }
      }
    }
  }
  return result.length > 0 ? result.substring(0, result.length - 1) : ''
}

/**
 *  sysdict 字典表
 * @param type 类型
 * @returns {string|*} value值
 */
export const SysDictOptions = (type) => {
  if (type !== undefined && type !== '') {
    const dictListForType = store.getters.sysDictListForType(type)
    if (dictListForType) {
      const tempDist = {}
      for (const key in dictListForType) {
        tempDist[key] = dictListForType[key]
      }
      return tempDist
    }
  }
  return []
}

/**
 * sysdict 字典表
 * @param key key
 * @param type 类型
 * @returns {string|*} value值
 */
export const SysDictOptionsFirst = (type) => {
  if (type !== undefined && type !== '') {
    const dictListForType = store.getters.sysDictListForType(type)
    if (dictListForType) {
      for (const key in dictListForType) {
        if (/^\d+$/.test(dictListForType[key].k)) {
          return parseInt(dictListForType[key].k)
        } else {
          return
        }
      }
    }
  }
  return []
}
export const SysDictOptionsFirstV = (type) => {
  if (type !== undefined && type !== '') {
    const dictListForType = store.getters.sysDictListForType(type)
    if (dictListForType) {
      for (const key in dictListForType) {
        if (/^\d+$/.test(dictListForType[key].v)) {
          return parseInt(dictListForType[key].v)
        } else {
          return
        }
      }
    }
  }
  return []
}

/**
 * 根据部门ID获取部门名称
 * @param key key
 * @param type 类型
 * @returns {string|*} value值
 */
export const GetDepartmentName = (departCode) => {
  if (departCode !== undefined && departCode !== '') {
    return store.getters.getDepartmentName(departCode)
  }
  return ''
}

/**
 * 获取岗位名称
 * @param key key
 * @param type 类型
 * @returns {string|*} value值
 */
export const GetPostionName = (postCode) => {
  if (postCode !== undefined && postCode !== '') {
    return store.getters.getPostionName(postCode)
  }
  return ''
}

/**
 * 根据部门ID获取最近部门的公司名称
 * @param key key
 * @param type 类型
 * @returns {string|*} value值
 */
export const GetDepartmentOrgName = (departCode) => {
  if (departCode !== undefined && departCode !== '') {
    return store.getters.getDepartmentOrgName(departCode)
  }
  return ''
}

/**
 * 根据部门ID获取最近部门的公司departCode
 * @param key key
 * @param type 类型
 * @returns {string|*} value值
 */
export const GetDepartmentOrgDepartCode = (departCode) => {
  if (departCode !== undefined && departCode !== '') {
    return store.getters.getDepartmentOrgDepartCode(departCode)
  }
  return ''
}

/**
 * 根据部门ID获取最近部门的项目名称
 * @param key key
 * @param type 类型
 * @returns {string|*} value值
 */
export const GetDepartmentProjectName = (departCode) => {
  if (departCode !== undefined && departCode !== '') {
    return store.getters.getDepartmentProjectName(departCode)
  }
  return ''
}

/**
 * 日期格式化
 * @param date
 * @param fmt
 * @returns {*|void|string}
 * @constructor
 */
export const DateFormat = (date, fmt) => {
  var o = {
    'M+': date.getMonth() + 1, // 月份
    'd+': date.getDate(), // 日
    'h+': date.getHours(), // 小时
    'm+': date.getMinutes(), // 分
    's+': date.getSeconds() // 秒
  }
  if (/(y+)/.test(fmt)) {
    fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
  }
  for (var k in o) {
    if (new RegExp('(' + k + ')').test(fmt)) {
      fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)))
    }
  }
  return fmt
}

export const MomentFormat = (date, fmt) => {
  return moment(date).format(fmt)
}

/**
 * 获取文件访问路径
 * @param avatar
 * @param imgerver
 * @param str
 * @returns {*}
 */
export const GetFileAccessHttpUrl = (avatar, imgerver, subStr) => {
  if (!imgerver) {
    imgerver = process.env.VUE_APP_API_BASE_URL
  }
  if (!subStr) {
    subStr = 'http'
  }
  if (avatar && avatar.indexOf(subStr) !== -1) {
    return avatar
  } else {
    return imgerver.substring(0, imgerver.length - 1) + avatar
  }
}

/**
 * 列表去重
 * @param avatar
 * @param imgerver
 * @param str
 * @returns {*}
 */
export const UniqueTable = (selectionRowsOld, selectionRowsNew, selectedRowKeys, uniqueKey) => {
  // 定义常量 res,值为一个Map对象实例
  Array.prototype.push.apply(selectionRowsOld, selectionRowsNew)
  const res = new Map()
  // 返回arr数组过滤后的结果，结果为一个数组
  // 过滤条件是，如果res中没有某个键，就设置这个键的值为1
  return selectionRowsOld.filter((a) => !res.has(a[uniqueKey]) && res.set(a[uniqueKey], 1) && selectedRowKeys.includes(a[uniqueKey]))
}

/**
 * 数组去重
 * @param avatar
 * @param imgerver
 * @param str
 * @returns {*}
 */
export const UniqueTableId = (selectedRowKeysOld, selectedRowKeysNew) => {
  // 定义常量 res,值为一个Map对象实例
  Array.prototype.push.apply(selectedRowKeysOld, selectedRowKeysNew)
  selectedRowKeysNew = unique(selectedRowKeysOld)
  return selectedRowKeysNew
}

function unique (arr) {
  var res = []
  var obj = {}
  for (var i = 0; i < arr.length; i++) {
    if (!obj[arr[i]]) {
      obj[arr[i]] = 1
      res.push(arr[i])
    }
  }
  return res
}

/**
 * 置换颜色
 * @param o1
 * @param o2
 * @returns {boolean}
 */
export function TagSourceFlag (data) {
  // 公共号 0
  // 门诊号 1
  // 住院号 2
  // 保留号 3
  // 第三方号 4
  // 双院转诊 5
  if (data) {
    data = parseInt(data)
  }
  if (data === 0) {
    return '#2db7f5'
  }
  if (data === 1) {
    return '#87d068'
  }
  if (data === 2) {
    return '#108ee9'
  }
  if (data === 3) {
    return '#f50'
  }
  if (data === 4) {
    return '#c960d9'
  }
  if (data === 5) {
    return '#ee8a07'
  }
  return '#FFFFFF'
}
